package com.jswlw.service.biz.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jswlw.dao.biz.DistributionAccountsMapper;
import com.jswlw.entity.biz.DistributionAccounts;
import com.jswlw.entity.biz.DistributionRecord;
import com.jswlw.service.biz.DistributionAccountsService;
import com.jswlw.service.biz.DistributionRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Service
public class DistributionAccountsServiceImpl implements DistributionAccountsService {

    @Autowired
    DistributionAccountsMapper mapper;
    @Autowired
    DistributionRecordService distributionRecordService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DistributionAccounts distributionAccounts) {
        //处理掉已经结算日期的数据
        mapper.insert(distributionAccounts);
        DistributionRecord filter=new DistributionRecord();
        filter.setStatus(0);
        filter.setStartTime(distributionAccounts.getStartTime());
        filter.setEndTime(distributionAccounts.getEndTime());
        distributionRecordService.queryList(filter).forEach(distributionRecord -> {
            distributionRecord.setStatus(1);
            distributionRecordService.update(distributionRecord);
        });
    }

    @Override
    public void update(DistributionAccounts distributionAccounts) {
        mapper.updateByPrimaryKeySelective(distributionAccounts);
    }

    @Override
    public void accounts(Integer id) {
        //结算
        DistributionAccounts distributionAccounts=mapper.selectByPrimaryKey(id);
        distributionAccounts.setStatus(1);
        mapper.updateByPrimaryKeySelective(distributionAccounts);


    }

    @Override
    public DistributionAccounts find4Id(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public void chgStatus(Integer id, Integer status) {

    }

    @Override
    public Page<DistributionAccounts> queryPage(DistributionAccounts distributionAccounts) {
        PageHelper.startPage(distributionAccounts.getOffset(), distributionAccounts.getLimit());
        return (Page<DistributionAccounts>) mapper.queryList(distributionAccounts);
    }

    @Override
    public List<DistributionAccounts> queryList(DistributionAccounts filter) {
        return mapper.queryList(filter);
    }
}
